package org.ewaf.framework.workflow.activemq;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

import net.sf.json.JSONObject;

import org.ewaf.framework.workflow.message.WorkflowMessage;
import org.springframework.jms.support.converter.MessageConversionException;
import org.springframework.jms.support.converter.MessageConverter;

public class WorkflowMessageConverter implements MessageConverter {

	@Override
	public Object fromMessage(Message message) throws JMSException,
			MessageConversionException {

		TextMessage  textMessage = (TextMessage) message;
		JSONObject jsonObject = JSONObject.fromObject(textMessage.getText());
		return JSONObject.toBean(jsonObject);
	}

	@Override
	public Message toMessage(Object object, Session session)
			throws JMSException, MessageConversionException {

		WorkflowMessage wfMessage = (WorkflowMessage) object;
		JSONObject jsObject = JSONObject.fromObject(wfMessage);

		TextMessage textMessage = session
				.createTextMessage(jsObject.toString());

		return textMessage;
	}

}
